package com.example.home_stretch;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QR_code extends AppCompatActivity {
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.iv_cq1)
    ImageView ivCq1;
    @BindView(R.id.bu_generate1)
    Button buGenerate1;
    @BindView(R.id.iv_cq2)
    ImageView ivCq2;
    @BindView(R.id.bu_generate2)
    Button buGenerate2;
    @BindView(R.id.ed_content)
    EditText edContent;
    @BindView(R.id.iv_cq3)
    ImageView ivCq3;
    //这个就是二维码的封装类
    private QR_code_one qr_code_one=new QR_code_one();
    private Bitmap zxing;
    private Uri uri;
    private String qwe,qwe1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr_code);
        ButterKnife.bind(this);
        initdata();
    }
    //随机数的二维码
    private void initview() {
        //生成随机数
        Random random=new Random();
        int num=random.nextInt(100)%(100-0+1)+1;
        zxing=qr_code_one.Zxing(String.valueOf(num));
        ivCq1.setImageBitmap(zxing);
    }
    //获取一个网页来生成二维码
    private void initdata(){
        uri=Uri.parse("https://blog.csdn.net/xch_yang/article/details/82147461");
        zxing=qr_code_one.Zxing(uri.toString());
        ivCq2.setImageBitmap(zxing);
        qwe=qr_code_one.Scanning(qwe,zxing);
        ivCq2.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            //长按生成一个弹窗来进入二维码的内容
            public boolean onLongClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(QR_code.this);
                builder.setMessage("扫描此二维码")
                        .setPositiveButton("扫描", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //点击跳转网页
                                Intent intent=new Intent(Intent.ACTION_VIEW);
                                intent.putExtra("http",Uri.parse(qwe).toString());
                                startActivity(intent);
                                tvContent.setText(qwe);
                            }
                        })
                    .create().show();
                return true;
            }
        });
    }
    @OnClick({R.id.bu_generate1, R.id.bu_generate2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bu_generate1:
                //扫描二维码
                initview();
                qwe1=qr_code_one.Scanning(qwe1,zxing);
                tvContent.setText(qwe1);
                break;
                //点击生成二维码
            case R.id.bu_generate2:
                //获取输入框的数据
                String content=edContent.getText().toString();
                if (content.equals("")){
                    Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
                }else{
                    zxing=qr_code_one.Zxing(content);
                    ivCq3.setImageBitmap(zxing);
                }
                break;
        }
    }
}
